package com.seu.me.zookeeper.zkclient.watcher;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

/**
 * @author liangfeihu
 * @since 2017/12/5 19:18.
 */
public class ZkClientWatcher2 {
    /**
     * zookeeper地址
     */
    static final String CONNECT_ADDR = "192.168.169.129:2181,192.168.169.130:2181,192.168.169.131:2181";
    /**
     * session超时时间 ms
     */
    static final int SESSION_TIMEOUT = 5000;

    /**
     * main 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_TIMEOUT);

        zkc.createPersistent("/super1", "1234");

        //对父节点添加监听子节点变化。
        zkc.subscribeDataChanges("/super1", new IZkDataListener() {
            @Override
            public void handleDataDeleted(String path) throws Exception {
                System.out.println("删除的节点为:" + path);
            }

            @Override
            public void handleDataChange(String path, Object data) throws Exception {
                System.out.println("变更的节点为:" + path + ", 变更内容为:" + data);
            }
        });

        Thread.sleep(3000);
        zkc.writeData("/super1", "456", -1);
        Thread.sleep(1000);

        zkc.delete("/super1");
        Thread.sleep(Integer.MAX_VALUE);

    }
}
